package com.znyj.servlet.dao;

import com.znyj.servlet.entity.ScheduledEntity;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.sql.Timestamp;
import java.util.List;

/**
 * Created by gaoliwei02 on 2017/11/1.
 */
@Repository
public interface ScheduledDao {
    public static final String SELECT_KEYS = " " +
            "id," +
            "user_id," +
            "device_id," +
            "create_time," +
            "duration_sec," +
            "control_msg," +
            "user_location," +

            "execute_result," +
            "open_orderId," +
            "extra_delay";
    public static final String INSERT_KEYS = " " +
            "user_id," +
            "device_id," +
            "create_time," +
            "duration_sec," +
            "control_msg," +
            "user_location," +

            "execute_result," +
            "open_orderId," +
            "extra_delay";
    public static final String INSERT_VALUES = " " +
            "#{userId}," +
            "#{deviceId}," +
            "#{createTime}," +
            "#{durationSec}," +
            "#{controlMsg}," +
            "#{userLocation}," +

            "#{executeResult}," +
            "#{openOrderId}," +
            "#{extraDelay}";
    public static final String SIMPLE_SELECT_KEYS = "id,device_id";

    @Insert("insert into cdb_scheduled (" + INSERT_KEYS + ") values (" + INSERT_VALUES + ") ")
    public int insert(ScheduledEntity entity);

    @Update("update cdb_scheduled set " +
            "user_id=#{userId}," +
            "device_id=#{deviceId}" +
            "create_time=#{createTime}," +
            "duration_sec=#{durationSec}," +
            "control_msg=#{controlMsg}," +
            "user_location=#{userLocation}," +

            "execute_result=#{executeResult}," +
            "extra_delay=#{extraDelay} " +
            "where open_orderId=#{openOrderId}")
    public int update(ScheduledEntity entity);

    @Select(" select " + SIMPLE_SELECT_KEYS + " from cdb_scheduled where device_id=#{deviceId}")
    public ScheduledEntity findByDeviceId(@Param("deviceId") String deviceId);

    @Select(" select * from cdb_scheduled where open_orderId=#{openOrderId}")
    public ScheduledEntity findByOpenOrderId(@Param("openOrderId") long openOrderId);

    @Update("update cdb_scheduled set " +
            "execute_result=#{executeResult} " +
            "where open_orderId=#{openOrderId}")
    public int updateExecuteResult(@Param("executeResult") String executeResult,
                      @Param("openOrderId") long openOrderId);

    @Update("update cdb_scheduled set " +
            "extra_delay=#{extraDelay} " +
            "where open_orderId=#{openOrderId}")
    public int updateOpenOrderId(@Param("extraDelay") long extraDelay,
                                 @Param("openOrderId") long openOrderId);

    @Select("SELECT m1.* FROM cdb_scheduled m1 LEFT JOIN cdb_scheduled m2 ON (m1.device_id = m2.device_id AND m1.id < m2.id) WHERE m2.id IS NULL")
    public List<ScheduledEntity> initScheduled();

}
